package com.alibaba.algo.domain.user;


import com.alibaba.algo.cooperation.feign.vo.req.GetGroupBaseInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectGroupPageRequestVO;
import com.alibaba.algo.cooperation.feign.vo.res.GroupBaseInfoResponseVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.EmployeeInfo;
import com.alibaba.algo.core.base.*;
import com.alibaba.algo.dao.user.SysGroupMapper;
import com.alibaba.algo.entity.user.SysGroup;
import com.alibaba.algo.entity.user.SysOrgGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 类SysGroup.java的实现描述：用户组Domain
 *
 * @author fan.tianpeng 2021-05-06 19:26:33
 */
@Component
public class SysGroupDomain extends BaseDomain<SysGroup> {

    @Resource
    private SysGroupMapper sysGroupMapper;

    @Autowired
    private SysEmployeeGroupDomain sysEmployeeGroupDomain;

    @Autowired
    private SysOrgGroupDomain sysOrgGroupDomain;

    @Autowired
    private SysEmployeeOrgDomain sysEmployeeOrgDomain;

    @Autowired
    private SysOrgDomain sysOrgDomain;

    /**
     * 通过用户组列表查询用户组信息
     *
     * @param groupIdList 用户组标识
     * @return 用户组集合信息
     */
    public Map<Long, SysGroup> qryUserGroupMapByIdList(List<Long> groupIdList) {
        if (CollectionUtils.isEmpty(groupIdList)) {
            return Collections.emptyMap();
        }
        SysGroup sysGroup = new SysGroup();
        sysGroup.setGroupIdList(groupIdList);
        List<SysGroup> sysGroupList = sysGroupMapper.listByPara(sysGroup, null);
        return sysGroupList.stream().collect(Collectors.toMap(BaseEntity::getId, k -> k, (v1, v2) -> v2));
    }

    /**
     * 1、通过用户工号/姓名/账号查询人员组ID清单
     * 2、通过员工所属的组织查询对应的人员组ID清单
     * 返回1&2的结果并集
     *
     * @param vo 查询条件对象
     * @return 人员组ID清单
     */
    public Set<Long> getGroupIdSetByEmployeeCondition(SelectGroupPageRequestVO vo) {
        Set<Long> groupIdSet = new HashSet<>();
        if (!ObjectUtils.isEmpty(vo.getEmployeeName())
                || !ObjectUtils.isEmpty(vo.getUserAccount())
                || !ObjectUtils.isEmpty(vo.getEmployeeCode())
                || (null != vo.getEmployeeId() && vo.getEmployeeId() > 0)
                || !ObjectUtils.isEmpty(vo.getUserName())
                || (null != vo.getUserId() && vo.getUserId() > 0)
                || !ObjectUtils.isEmpty(vo.getSearchKey())
        ) {
            //查询直接关联的员工对应的用户组
            List<EmployeeInfo> employeeList = sysEmployeeGroupDomain.selectListWithGroupIdByGroupCondition(null,
                    vo.getEmployeeName(),
                    vo.getUserAccount(),
                    vo.getEmployeeCode(),
                    vo.getEmployeeId(),
                    vo.getUserName(),
                    vo.getUserId(),
                    vo.getSearchKey());
            if (!ObjectUtils.isEmpty(employeeList)) {
                for (EmployeeInfo employeeInfo : employeeList) {
                    groupIdSet.add(employeeInfo.getGroupId());
                }
            }
            //查询组织下员工对应的用户组
//            List<Long> orgIdList = sysEmployeeOrgDomain.selectOrgIdListByCondition(vo.getEmployeeName(),
//                    vo.getUserAccount(),
//                    vo.getEmployeeCode(),
//                    vo.getEmployeeId(),
//                    vo.getUserName(),
//                    vo.getUserId(),
//                    vo.getSearchKey()).stream().distinct().collect(Collectors.toList());
//            Set<Long> orgIdSet = new HashSet<>();
//            if (!ObjectUtils.isEmpty(orgIdList)) {
//                for (Long orgId : orgIdList) {
//                    if (orgIdSet.contains(orgId)) {
//                        continue;
//                    }
//                    List<Long> orgIdReCursiveList = sysOrgDomain.selectOrgIdListRecursiveToRootCorrelateGroup(orgId);
//                    orgIdSet.addAll(orgIdReCursiveList);
//                }
//                if (!ObjectUtils.isEmpty(orgIdSet)) {
//                    List<SysOrgGroup> sysGroupList = sysOrgGroupDomain.selectByOrgIdList(new ArrayList<>(orgIdSet));
//                    if (!ObjectUtils.isEmpty(sysGroupList)) {
//                        for (SysOrgGroup sysOrgGroup : sysGroupList) {
//                            if (null != sysOrgGroup.getGroupId()) {
//                                groupIdSet.add(sysOrgGroup.getGroupId());
//                            }
//                        }
//                    }
//                }
//            }
        }
        Long groupId = vo.getGroupId();
        if (!ObjectUtils.isEmpty(groupId)) {
            groupIdSet.add(groupId);
        }
        List<Long> groupIdList = vo.getGroupIdList();
        if (!ObjectUtils.isEmpty(groupIdList)) {
            groupIdSet.addAll(groupIdList);
        }
        return groupIdSet;
    }

    public List<SysGroup> getListByIds(List<Long> ids) {
        return sysGroupMapper.getListByIds(ids);
    }

    public PagerEntity<SysGroup> pageByParaWithEmployeeCondition(SysGroup sysGroup, Integer pageIndex, Integer pageSize, LinkedHashMap<String, Object> sortMap) {
        long total = sysGroupMapper.countByParaWithEmployeeCondition(sysGroup);
        Pager pager = Pager.newInstance(total, pageIndex, pageSize);
        List<SysGroup> result = sysGroupMapper.pageByParaWithEmployeeCondition(sysGroup, pager.getStart(), pager.getEnd(), CoreTools.getSortStr(sortMap));
        return new PagerEntity<>(total, pager.getTotalPages(), pageSize, pageIndex, result);
    }

    public int updateDeleteFlagByGroupIds(SysGroup sysGroup, List<Long> ids) {
        return sysGroupMapper.updateDeleteFlagByGroupIds(sysGroup, ids);
    }

    /**
     * 通过人员组Id或Code查询人员组基本信息
     *
     * @param paramVO
     * @return
     */
    public GroupBaseInfoResponseVO getGroupBaseInfoByIdOrCode(GetGroupBaseInfoReqVO paramVO) {
        return sysGroupMapper.getGroupBaseInfoByIdOrCode(paramVO);
    }

    public List<SysGroup> getListCodeOrName(String groupCode, String groupName) {
        return sysGroupMapper.getListCodeOrName(groupCode, groupName);
    }

    public SysGroup getByCode(String groupCode) {
        return sysGroupMapper.getByCode(groupCode);
    }

    public void save(SysGroup sysGroup) {
        SysGroup group = getByCode(sysGroup.getGroupCode());
        if (!ObjectUtils.isEmpty(group)) {
            return;
        }
        sysGroup.setIsDeleted(YesOrNo.N.name());
        this.insert(sysGroup);
    }
}
